'''
String Library
A part of the libPirate (Python) library

Author:    Derrick Sobodash <derrick@cinnamonpirate.com>
Copyright: (c) 2006, 2007, 2008 Derrick Sobodash
Web site:  http://libpirate.org/
License:   BSD License <http://opensource.org/licenses/bsd-license.php>
'''

'''
strsub()

Substitute one string into another at a a given offset. Can optionally
trim after a size= number of characters.
'''
def strsub(haystack, pos, needle, size=None):
	if len != None and len(needle) > size:
		needle = needle[0:size]
	return(haystack[0:pos] + needle + haystack[pos + len(needle):])

'''
strxor()

Returns an XOR of the supplied stringusing the suppled XOR string.
The XOR string can be any length. It will be mirrored as the string is
processed. The xor must also be a byte string. This means passing
0x9f is not acceptable. You must pass "\x9f" or chr(0x9f).
'''
def strxor(string, xor):
	if len(xor) < len(string):
		temp = xor
		while len(xor) < len(string):
			xor += temp
		xor = xor[0:len(string)]
	xstring = ""
	for i in range(0, len(string)):
		xstring += chr(ord(string[i]) ^ ord(xor[i]))
	return(xstring)


